from collections import deque
from enum import IntEnum, auto
from itertools import count
from typing import Deque, Sequence, Union


class MiddlewareLocation(IntEnum):
    REQUEST = auto()
    RESPONSE = auto()


# TODO: 所有的中间件可以只用这一个类来处理
class Middleware:
    ...


async def middleware(request, handler):
    print('Before handling request')
    response = await handler(request)
    print('After handling request')
    return response


def log_middleware(handler):
    def _inner(*args, **kwargs):
        # 在处理请求之前进行日志记录
        print("Logging request...")
        # 调用下一个处理程序（可能是其他中间件或最终的请求处理函数）
        response = handler(*args, **kwargs)
        # 在处理请求之后进行日志记录
        print("Logging response...")
        return response
    return _inner
